# Build shared pprz math library
#

# Quiet compilation
Q=@

CC= gcc
CFLAGS= -fpic
INCLUDES= -I$(PAPARAZZI_SRC)/sw/include -I$(PAPARAZZI_SRC)/sw/airborne

ifeq ($(PAPARAZZI_HOME),)
PAPARAZZI_HOME=$(PAPARAZZI_SRC)
endif

# build in $(PAPARAZZI_HOME)/var/build/math
BUILDDIR ?= $(PAPARAZZI_HOME)/var/build/math

PREFIX ?= /usr/local

LIB_INSTALLDIR=${PREFIX}/lib
INCLUDE_INSTALLDIR=${PREFIX}/include/pprz
PKGCONFIG_INSTALLDIR=${PREFIX}/lib/pkgconfig
PKGCONFIG_FILE=pprzmath.pc

SRC= $(wildcard *.c)
OBJ= $(addprefix $(BUILDDIR)/,$(SRC:.c=.o))

LIBNAME=libpprzmath

all:
	@cat README

shared_lib: $(OBJ)
	$(CC) -shared -o $(BUILDDIR)/$(LIBNAME).so $(OBJ)

install_shared_lib: shared_lib
	test -d $(LIB_INSTALLDIR) || mkdir -p $(LIB_INSTALLDIR)
	cp $(BUILDDIR)/$(LIBNAME).so $(LIB_INSTALLDIR)
	test -d $(INCLUDE_INSTALLDIR)/math || mkdir -p $(INCLUDE_INSTALLDIR)/math
	cp *.h $(INCLUDE_INSTALLDIR)/math
	cp ../../include/std.h $(INCLUDE_INSTALLDIR)
	test -d $(PKGCONFIG_INSTALLDIR) || mkdir -p $(PKGCONFIG_INSTALLDIR)
	sed -e 's#PREFIX#$(PREFIX)#g' $(PKGCONFIG_FILE).in > $(PKGCONFIG_INSTALLDIR)/$(PKGCONFIG_FILE)

$(BUILDDIR)/%.o: %.c
	test -d $(BUILDDIR) || mkdir -p $(BUILDDIR)
	$(CC) -c $< $(CFLAGS) $(INCLUDES) -o $@

clean:
	$(Q)rm -f $(BUILDDIR)/*.o $(BUILDDIR)/$(LIBNAME).so

.PHONY: all clean shared_lib install_shared_lib
